package com.qf.main.pharmacy.controller;

import com.qf.common.base.result.ResponseResult;
import com.qf.main.pharmacy.common.qo.OrderMallQo;
import com.qf.main.pharmacy.common.qo.OrderQo;
import com.qf.main.pharmacy.common.vo.OrderVo;
import com.qf.main.pharmacy.entity.OrderMall;
import com.qf.main.pharmacy.service.OrderMallService;
import com.qf.main.pharmacy.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chengqiang
 * @Create 2024-02-19-9:28
 * @Description:
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Api(tags = "订单管理")
public class OrderController {
    @Resource
    OrderService orderService;
    @Resource
    OrderMallService orderMallService;




    @PostMapping("/save")
    public ResponseResult<String> saveOrder(@RequestBody OrderQo orderQo){
        return ResponseResult.success(String.valueOf(orderService.saveOrder(orderQo)));
    }
    @GetMapping("/list")
    public ResponseResult<List<OrderVo>> OrderList(@RequestParam(value = "pageNum" ,required = false) Integer pageNum, @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                   @RequestParam("userId") int userId){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        List<OrderVo> orderVos = orderService.orderByIdList(pageNum, pageSize, userId);
        return ResponseResult.success(orderVos);
    }
    @PutMapping("/erase/address")
    public ResponseResult<Integer> eraseOrderAddress(@RequestBody OrderQo orderQo){
        OrderQo address = new OrderQo();
        address.setOrderNo(orderQo.getOrderNo());
        address.setOrderAddress(orderQo.getOrderAddress());
        return ResponseResult.success(orderService.modifyOrder(address));
    }
    @GetMapping("/list/all")
    public ResponseResult<List<OrderVo>> OrderListAll(@RequestParam(value = "pageNum" ,required = false) Integer pageNum, @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                   @RequestParam(value = "orderNo" ,required = false) String orderNo,@RequestParam(value = "status" ,required = false) Integer status){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        List<OrderVo> orderVos;
        if(status==null){
             orderVos = orderService.orderList(pageNum, pageSize, orderNo,null);
        }else
        if(status==0){
             orderVos = orderService.orderList(pageNum, pageSize, orderNo,0);
        }else if(status==1){
            orderVos = orderService.orderList(pageNum, pageSize, orderNo,1);
        }else if(status==2){
             orderVos = orderService.orderList(pageNum, pageSize, orderNo,2);
        }else {
             orderVos = orderService.orderList(pageNum, pageSize, orderNo,null);
        }

        return ResponseResult.success(orderVos);
    }

    @PutMapping("/erase/status")
    public ResponseResult<Integer> statusOrderAddress(@RequestBody OrderQo orderQo){
        OrderQo status = new OrderQo();
        status.setOrderNo(orderQo.getOrderNo());
        status.setOrderStatus(orderQo.getOrderStatus());
        return ResponseResult.success(orderService.modifyOrder(status));
    }
    @PutMapping("/erase/mall/status")
    public ResponseResult<Integer> statusOrderMallAddress(@RequestBody OrderMallQo orderMallQo){
        OrderMallQo orderMall = new OrderMallQo();
        orderMall.setId(orderMallQo.getId());
        orderMall.setOrderMallStatus(orderMallQo.getOrderMallStatus());
        return ResponseResult.success(orderMallService.modifyOrderMall(orderMall));
    }
    @DeleteMapping("/edit")
    public ResponseResult<Integer> editOrder(@RequestParam("orderId") int orderId){
        return ResponseResult.success(orderService.removeOrder(orderId));
    }
    @DeleteMapping("/recover")
    public ResponseResult<Integer> recoverOrder(@RequestParam(value = "id" )Integer id){
        return ResponseResult.success(orderService.recoverOrder(id));
    }
    @GetMapping("/cancel")
    public ResponseResult<Integer> cancelOrder(@RequestParam(value = "orderNo" )String orderNo){
        return ResponseResult.success(orderService.cancelOrder(orderNo));
    }


}
